package com.etoak.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.etoak.common.core.exception.CustomException;
import com.etoak.common.core.properties.ImageProperties;
import com.etoak.common.core.vo.PageVO;
import com.etoak.entity.Ingredients;
import com.etoak.mapper.IngredientsMapper;
import com.etoak.service.IngredientsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author etoak
 * @since 2025-07-28
 */
@Service
public class IngredientsServiceImpl extends ServiceImpl<IngredientsMapper, Ingredients> implements IngredientsService {

    @Autowired
    ImageProperties imageProperties;

    @Override
    public void add(Ingredients ingredients) {
        if (ObjectUtils.isNotEmpty(getByName(ingredients.getName()))) {
            throw new CustomException("食材名称重复！");
        }

        ingredients.setCreateTime(DateUtil.now());
        this.save(ingredients);
    }

    @Override
    public List<Ingredients> getList(Ingredients ingredients) {
        return lambdaQuery()
                .likeRight(StringUtils.isNotEmpty(ingredients.getName()), Ingredients::getName, ingredients.getName())
                .eq(StringUtils.isNotEmpty(ingredients.getType()), Ingredients::getType, ingredients.getType())
                .list();
    }

    @Override
    public PageVO<Ingredients> listPage(int pageNum, int pageSize, Ingredients ingredients) {
        PageHelper.startPage(pageNum, pageSize);
        List<Ingredients> ingredientsList = this.getList(ingredients);
        PageInfo<Ingredients> pageInfo = new PageInfo<>(ingredientsList);
        return new PageVO<>(ingredientsList, pageInfo.getTotal());
    }

    @Override
    public void updateIngredients(int id, Ingredients ingredients) {
        // 如果根据 id 查不到数据，则不做修改
        if (ObjectUtils.isEmpty(getById(id))) {
            throw new CustomException("食材不存在!");
        }

        String ingredientsName = ingredients.getName();
        if (StringUtils.isNotEmpty(ingredientsName)) {
            //根据 name 查询食材信息
            Ingredients savedIngredients = getByName(ingredientsName);
            if (ObjectUtils.isNotEmpty(savedIngredients) && !savedIngredients.getId().equals(id)) {
                throw new CustomException("不能将名称修改为其他食材的名称!");
            }
        }

        ingredients.setId(id);
        ingredients.setUpdateTime(DateUtil.now());
        this.updateById(ingredients);
    }

    @Override
    public void delete(Integer id) {
        Ingredients ingredients = getById(id);
        // 如果根据 id 查不到数据，则不做删除
        if (ObjectUtils.isEmpty(ingredients)) {
            throw new CustomException("食材不存在！");
        }
        String url = ingredients.getUrl();
        String fileName = url.substring(url.lastIndexOf("/") + 1);

        File file = new File(imageProperties.getUploadDir(), fileName);
        if (file.exists()) {
            file.delete();
        }

        this.removeById(id);
    }

    /**
     * 根据食材名称 查询食材信息
     *
     * @param name 食材名称
     * @return 食材信息
     */
    private Ingredients getByName(String name) {
        return lambdaQuery().eq(Ingredients::getName, name).one();
    }
}
